﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using SafoverWpfFw;
using System;
using System.IO;
using System.Windows.Input;
using Safover.LidarCollector.Core;
using Safover.LidarCollector.Managers;
using Safover.LidarCollector.Config;
using static Safover.LidarCollector.ViewModel.DI;

// TODO rename
namespace Safover.LidarCollector.ViewModel
{
    public class CameraPageViewModel : BaseViewModel
    {
        private ICamera Camera
        {
            get { return DeviceManager.DeviceFactoryInstance.Camera; }
        }

        public IntPtr PlayWnd { get; set; }

        public ICommand GoBackCommand { get; set; }

        public ICommand StartVideoCommand { get; set; }

        public ICommand StopVideoCommand { get; set; }

        public ICommand CaptureImageCommand { get; set; }

        public ICommand ClearImageCommand { get; set; }

        public string CurrentImagePath { get; set; }

        public CameraPageViewModel()
        {
            GoBackCommand = new RelayCommand(() => ViewModelApplication.GoToPage(ApplicationPage.Collection));

            StartVideoCommand = new RelayCommand(StartVideo);
            StopVideoCommand = new RelayCommand(StopVideo);
            CaptureImageCommand = new RelayCommand(CaptureImage);
            ClearImageCommand = new RelayCommand(() => CurrentImagePath = string.Empty);
        }

        private void CaptureImage()
        {
            var fileName = $"{DateTime.Now:yyyy-MM-dd HH.mm.ss.ms}.jpg";
            var fullPath = Path.Combine(RuntimeConfigManager.Instance.OthersConfig.PictureFilePath, fileName);

            if (Camera.CaptureImage(ImageType.JPE, fullPath))
            {
                CurrentImagePath = fullPath;
            }
            else
            {
                CurrentImagePath = string.Empty;
            }
        }

        private void StopVideo()
        {
            Camera.StopVideo();
        }

        private void StartVideo()
        {
            Camera.StartVideo(PlayWnd);
        }
    }
}
